package com.feng.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.reggie.common.CustomException;
import com.feng.reggie.entity.Category;
import com.feng.reggie.entity.Dish;
import com.feng.reggie.entity.Setmeal;
import com.feng.reggie.mapper.CategoryMapper;
import com.feng.reggie.service.CategoryService;
import com.feng.reggie.service.DIshService;
import com.feng.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    /**
     * 根据id删除分类，删除之前需要判断
     * @param id
     */

    @Autowired
    private DIshService dIshService;
    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        //查询当前分类是否关联了菜品，如果已经关联，抛出一个业务异常
        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<>();
        //添加查询条件，根据分类id进行查询
        queryWrapper.eq(Dish::getCategoryId,id);
        int count = dIshService.count(queryWrapper);
        if(count>0){
            //已经关联菜品，抛出一个业务异常
            throw new CustomException("当前分类项关联了菜品，不能删除！");
        }
        LambdaQueryWrapper<Setmeal>  queryWrapper2=new LambdaQueryWrapper<>();
        queryWrapper2.eq(Setmeal::getCategoryId,id);
        int count1 = setmealService.count(queryWrapper2);
        //查询当前分类是否关联了套餐，如果已经关联，抛出一个业务异常
          if(count1>0){
              throw new CustomException("当前分类项关联了套餐，不能删除！");
          }
        //正常删除分类
        super.removeById(id);
    }
}
